package com.example.demomk.Adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demomk.databinding.ItemPrintDetailBinding;
import com.example.demomk.entity.MeasurementSummary;

import java.util.ArrayList;
import java.util.List;

public class PrintDetailsAdapter extends RecyclerView.Adapter<PrintDetailsAdapter.ViewHolder>{
    private List<MeasurementSummary> list = new ArrayList<>();
    public void setDataList(List<MeasurementSummary> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public PrintDetailsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemPrintDetailBinding binding = ItemPrintDetailBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull PrintDetailsAdapter.ViewHolder holder, int position) {
        MeasurementSummary item = list.get(position);
        holder.location_name.setText(item.getArea_name());
        holder.point_count.setText(String.valueOf(item.getPoint_count()));
//        holder.measurement_ids.setText(item.getMeasurement_ids());
        String measurementIds = item.getMeasurement_ids();
        if (measurementIds != null && !measurementIds.isEmpty()) {
            String[] ids = measurementIds.split(",");
            if (ids.length == 1) {
                holder.measurement_ids.setText(ids[0]);
            } else {
                holder.measurement_ids.setText(ids[0] + "-" + ids[ids.length - 1]);
            }
        } else {
            holder.measurement_ids.setText(""); // 或者 "无数据"
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView location_name, point_count, measurement_ids;
        public ViewHolder(@NonNull ItemPrintDetailBinding binding) {
            super(binding.getRoot());
            location_name = binding.locationName;
            point_count = binding.count;
            measurement_ids = binding.ids;
        }
    }
}
